//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "SpreadedHazardRateCurve.h"
using namespace Cephei::QL::Experimental::Credit;
#include <gen/QL/Times/Calendar.h>
#include <gen/QL/Times/DayCounter.h>
#include <gen/QL/Termstructures/DefaultProbabilityTermStructure.h>
#include <gen/QL/Quote.h>
#include <gen/QL/Termstructures/Credit/HazardRateStructure.h>
using namespace Cephei::QL::Times;
using namespace Cephei::QL::Termstructures;
using namespace Cephei::QL;
using namespace Cephei::QL::Termstructures::Credit;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Credit::CSpreadedHazardRateCurve::CSpreadedHazardRateCurve (Cephei::QL::Termstructures::IDefaultProbabilityTermStructure^ originalCurve, Cephei::QL::IQuote^ spread) : CHazardRateStructure(CSpreadedHazardRateCurve::typeid)
{
    CDefaultProbabilityTermStructure^ _CoriginalCurve;
    CQuote^ _Cspread;
    try
    {
#ifdef HANDLE
        _phSpreadedHazardRateCurve = NULL;
#endif
        _CoriginalCurve = safe_cast<CDefaultProbabilityTermStructure^> (originalCurve);
        _CoriginalCurve->Lock();
        Handle<QuantLib::DefaultProbabilityTermStructure>& _originalCurve = static_cast<Handle<QuantLib::DefaultProbabilityTermStructure>&> (_CoriginalCurve->GetHandle ()); 
        _Cspread = safe_cast<CQuote^> (spread);
        _Cspread->Lock();
        Handle<QuantLib::Quote>& _spread = static_cast<Handle<QuantLib::Quote>&> (_Cspread->GetHandle ()); 
        _ppSpreadedHazardRateCurve = new boost::shared_ptr<QuantLib::SpreadedHazardRateCurve> (new QuantLib::SpreadedHazardRateCurve ( _originalCurve,  _spread ));
        SetHazardRateStructure (boost::dynamic_pointer_cast<QuantLib::HazardRateStructure> (*_ppSpreadedHazardRateCurve));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_CoriginalCurve != nullptr) _CoriginalCurve->Unlock();
        if (_Cspread != nullptr) _Cspread->Unlock();
    }
}
Cephei::QL::Experimental::Credit::CSpreadedHazardRateCurve::CSpreadedHazardRateCurve (boost::shared_ptr<QuantLib::SpreadedHazardRateCurve>& childNative, Object^ owner) : CHazardRateStructure(CSpreadedHazardRateCurve::typeid)
{
#ifdef HANDLE
	_phSpreadedHazardRateCurve = NULL;
#endif
	_ppSpreadedHazardRateCurve = &childNative;
    _ppHazardRateStructure = new boost::shared_ptr<QuantLib::HazardRateStructure> (boost::dynamic_pointer_cast<QuantLib::HazardRateStructure> (*_ppSpreadedHazardRateCurve));
}
Cephei::QL::Experimental::Credit::CSpreadedHazardRateCurve::CSpreadedHazardRateCurve (QuantLib::SpreadedHazardRateCurve& childNative, Object^ owner) : CHazardRateStructure(CSpreadedHazardRateCurve::typeid)
{
#ifdef HANDLE
	_phSpreadedHazardRateCurve = NULL;
#endif
	_ppSpreadedHazardRateCurve = new boost::shared_ptr<QuantLib::SpreadedHazardRateCurve> (&childNative);
    _ppHazardRateStructure = new boost::shared_ptr<QuantLib::HazardRateStructure> (boost::dynamic_pointer_cast<QuantLib::HazardRateStructure> (*_ppSpreadedHazardRateCurve));
    _SpreadedHazardRateCurveOwner = owner;
    _HazardRateStructureOwner = owner;
}

Cephei::QL::Experimental::Credit::CSpreadedHazardRateCurve::CSpreadedHazardRateCurve (CSpreadedHazardRateCurve^ copy) : CHazardRateStructure(CSpreadedHazardRateCurve::typeid)
{
#ifdef HANDLE
	_phSpreadedHazardRateCurve = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppSpreadedHazardRateCurve = new boost::shared_ptr<QuantLib::SpreadedHazardRateCurve> (copy->GetShared());
        _ppHazardRateStructure = new boost::shared_ptr<QuantLib::HazardRateStructure> (boost::dynamic_pointer_cast<QuantLib::HazardRateStructure> (*_ppSpreadedHazardRateCurve));
    }
}
Cephei::QL::Experimental::Credit::CSpreadedHazardRateCurve::CSpreadedHazardRateCurve (PLATFORM::Type^ t) : CHazardRateStructure(CSpreadedHazardRateCurve::typeid)
{
#ifdef HANDLE
	_phSpreadedHazardRateCurve = NULL;
#endif
	if (!t->IsSubclassOf(CSpreadedHazardRateCurve::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Credit::CSpreadedHazardRateCurve::CSpreadedHazardRateCurve (QuantLib::Handle<QuantLib::SpreadedHazardRateCurve>& childNative, Object^ owner)  : CHazardRateStructure(CSpreadedHazardRateCurve::typeid)
{
	_phSpreadedHazardRateCurve = &childNative;
	_ppSpreadedHazardRateCurve = &static_cast<boost::shared_ptr<QuantLib::SpreadedHazardRateCurve>>(childNative.currentLink());
    _ppHazardRateStructure = new boost::shared_ptr<QuantLib::HazardRateStructure> (boost::dynamic_pointer_cast<QuantLib::HazardRateStructure> (*_ppSpreadedHazardRateCurve));
    _SpreadedHazardRateCurveOwner = owner;
}
Cephei::QL::Experimental::Credit::CSpreadedHazardRateCurve::CSpreadedHazardRateCurve (QuantLib::Handle<QuantLib::SpreadedHazardRateCurve> childNative)  : CHazardRateStructure(CSpreadedHazardRateCurve::typeid)
{
	_phSpreadedHazardRateCurve = &childNative;
	_ppSpreadedHazardRateCurve = &static_cast<boost::shared_ptr<QuantLib::SpreadedHazardRateCurve>>(childNative.currentLink());
    _ppHazardRateStructure = new boost::shared_ptr<QuantLib::HazardRateStructure> (boost::dynamic_pointer_cast<QuantLib::HazardRateStructure> (*_ppSpreadedHazardRateCurve));
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Credit::CSpreadedHazardRateCurve::CSpreadedHazardRateCurve (QuantLib::SpreadedHazardRateCurve childNative)  : CHazardRateStructure(CSpreadedHazardRateCurve::typeid)
{
#ifdef HANDLE
	_phSpreadedHazardRateCurve = NULL;
#endif
	_ppSpreadedHazardRateCurve = new boost::shared_ptr<QuantLib::SpreadedHazardRateCurve> (new QuantLib::SpreadedHazardRateCurve (childNative));
    _ppHazardRateStructure = new boost::shared_ptr<QuantLib::HazardRateStructure> (boost::dynamic_pointer_cast<QuantLib::HazardRateStructure> (*_ppSpreadedHazardRateCurve));
}
#endif

Cephei::QL::Experimental::Credit::CSpreadedHazardRateCurve::~CSpreadedHazardRateCurve ()
{
    if (_ppSpreadedHazardRateCurve != NULL)
    {
	    delete _ppSpreadedHazardRateCurve;
        _ppSpreadedHazardRateCurve = NULL;
    }
}
Cephei::QL::Experimental::Credit::CSpreadedHazardRateCurve::!CSpreadedHazardRateCurve ()
{
    if (_ppSpreadedHazardRateCurve != NULL)
    {
	    delete _ppSpreadedHazardRateCurve;
    }
}
QuantLib::SpreadedHazardRateCurve& Cephei::QL::Experimental::Credit::CSpreadedHazardRateCurve::GetReference ()
{
    if (_ppSpreadedHazardRateCurve == NULL) throw REFNEW NativeNullException ();
	return **_ppSpreadedHazardRateCurve;
}
boost::shared_ptr<QuantLib::SpreadedHazardRateCurve>& Cephei::QL::Experimental::Credit::CSpreadedHazardRateCurve::GetShared ()
{
    if (_ppSpreadedHazardRateCurve == NULL) throw REFNEW NativeNullException ();
	return *_ppSpreadedHazardRateCurve;
}
QuantLib::SpreadedHazardRateCurve* Cephei::QL::Experimental::Credit::CSpreadedHazardRateCurve::GetPointer ()
{
    if (_ppSpreadedHazardRateCurve == NULL) throw REFNEW NativeNullException ();
	return &**_ppSpreadedHazardRateCurve;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::SpreadedHazardRateCurve>& Cephei::QL::Experimental::Credit::CSpreadedHazardRateCurve::GetHandle ()
{
	if (_phSpreadedHazardRateCurve == NULL)
	{
		_phSpreadedHazardRateCurve = new Handle<QuantLib::SpreadedHazardRateCurve> (*_ppSpreadedHazardRateCurve);
	}
	return *_phSpreadedHazardRateCurve;
}
#endif
bool Cephei::QL::Experimental::Credit::CSpreadedHazardRateCurve::HasNative () 
{
	return (_ppSpreadedHazardRateCurve != NULL);
}

Cephei::QL::Times::ICalendar^ Cephei::QL::Experimental::Credit::CSpreadedHazardRateCurve::Calendar::get ()
{
    try
    {
    	QuantLib::Calendar _rv = (QuantLib::Calendar)(*_ppSpreadedHazardRateCurve)->calendar ( );   
        Cephei::QL::Times::CCalendar^ _nrv = REFNEW Cephei::QL::Times::CCalendar (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Times::IDayCounter^ Cephei::QL::Experimental::Credit::CSpreadedHazardRateCurve::DayCounter::get ()
{
    try
    {
    	QuantLib::DayCounter _rv = (QuantLib::DayCounter)(*_ppSpreadedHazardRateCurve)->dayCounter ( );   
        Cephei::QL::Times::CDayCounter^ _nrv = REFNEW Cephei::QL::Times::CDayCounter (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
DateTime Cephei::QL::Experimental::Credit::CSpreadedHazardRateCurve::MaxDate::get ()
{
    try
    {
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppSpreadedHazardRateCurve)->maxDate ( );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Experimental::Credit::CSpreadedHazardRateCurve::MaxTime::get ()
{
    try
    {
    	QuantLib::Time _rv = (QuantLib::Time)(*_ppSpreadedHazardRateCurve)->maxTime ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
DateTime Cephei::QL::Experimental::Credit::CSpreadedHazardRateCurve::ReferenceDate::get ()
{
    try
    {
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppSpreadedHazardRateCurve)->referenceDate ( );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Experimental::Credit::ISpreadedHazardRateCurve^ Cephei::QL::Experimental::Credit::CSpreadedHazardRateCurve_Factory::Create (Cephei::QL::Termstructures::IDefaultProbabilityTermStructure^ originalCurve, Cephei::QL::IQuote^ spread)
{
    return REFNEW CSpreadedHazardRateCurve ( originalCurve,  spread);
}
